<?php
namespace Home\Controller;

use Think\Controller;

class ProblemController extends Controller
{
    
	
	//显示
	public function select()
	{
		
		$mod=M("problem");
		//判断并封装搜索条件
        $where=array();
		if(!empty($_GET['name'])){
            $map['name']=array("like","%{$_GET['name']}%");
			$this->assign("name",$_GET['name']);
		}
        if(!empty($_GET['desc'])){
            $map['desc']=array("like","%{$_GET['desc']}%");
			$this->assign("desc",$_GET['desc']);
		}
		if(!empty($_GET['issolve'])){
            $map['issolve']=array("like","%{$_GET['issolve']}%");
			$this->assign("issolve",$_GET['issolve']);
		}
		
		
        //实例化分页类
		//定义每页显示的条数
		$m=5;
        $page = new \Think\Page($mod->where($map)->count(),$m);
	    $list=$mod->where($map)->order("id desc")->limit($page->firstRow.",".$page->listRows)->select();
		$this->assign("list",$list);
		$page->setConfig('prev','上一页');
		$page->setConfig('next','下一页');
		

		
		$this->assign("pageinfo",$page->show());
		$this->display();
	}
	
	//加载添加模板
	public function add()
	{
		$this->display();
	}
	
	//加载添加方法
	public function insert()
	{
		
		
		 $mod=M("problem");
		 $data=array();
		
		 $data['name']=$_POST['name'];
		 $data['desc']=$_POST['desc'];
		 $data['create_time']=strtotime('now');
		 
		
		 $mod->create($data);
		 
		 $result=$mod->add();
		 if($result+0>0){   
			$this->success('新增成功', 'select',0);
		 } else {   
			$this->error('新增失败');
		 }
	}
	
	//加载修改模板
	public function edit($id)
	{
		$mod=M("problem");
		$list=$mod->find("{$id}");
		$this->assign("list",$list);
		$this->display();
	}
	
	//执行修改信息的方法
	public function update()
	{
		
		
			$mod=M("problem");
			$mod->create($_POST);
			$result=$mod->save();
			if($result+0>0){
				$this->success('修改成功', 'select',0);
			} else {   
				$this->error('修改失败');
				
			}
		
		
	}
	
	
	//执行修改信息的方法
	 public function del($id)
	 {
		 $mod=M("problem");
		 $result=$mod->delete($id);
		 if($result+0>0){
			
			 $this->success('删除成功', 'select',0);
		 } else {   
			 $this->error('删除失败');
		 }
	 }
	 
	
	 
	 
	 
	
}